package com.beyondsoft.common.activiti.service.impl;

import com.beyondsoft.common.activiti.exception.ActivitiException;
import com.beyondsoft.common.activiti.exception.ResultCode;
import com.beyondsoft.common.activiti.model.CommentData;
import com.beyondsoft.common.activiti.model.HistoryData;
import com.beyondsoft.common.activiti.model.TaskData;
import com.beyondsoft.common.activiti.model.WorkflowTaskData;
import com.beyondsoft.common.activiti.service.WorkflowService;
import com.beyondsoft.common.activiti.support.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author zhangyan
 * @version $ Id: WorkflowServiceImpl.java, v 0.1 2020/6/20 11:42 zhangyan Exp $
 */
@Service
@Slf4j
public class WorkflowServiceImpl implements WorkflowService {

    @Resource
    private StartProcessInstanceSupport startProcessInstanceSupport;

    @Resource
    private CompleteTaskSupport completeTaskSupport;

    @Resource
    private ClaimTaskSupport claimTaskSupport;

    @Resource
    private ShowProcessDiagramSupport showProcessDiagramSupport;

    @Resource
    private QueryTaskSupport queryTaskSupport;

    @Resource
    private QueryHistoricTaskSupport queryHistoricTaskSupport;

    @Resource
    private QueryToDoTaskSupport queryToDoTaskSupport;


    /**
     * 启动流程
     *
     * @param procDefKey
     * @return
     */
    @Override
    public String startByKey(String procDefKey) {
        return startByKey(null, procDefKey);
    }

    /**
     * 启动流程
     *
     * @param authenticateUserId
     * @param procDefKey
     * @return
     */
    @Override
    public String startByKey(String authenticateUserId, String procDefKey) {
        return this.startByKey(authenticateUserId, procDefKey, null);
    }

    /**
     * 启动流程
     *
     * @param authenticateUserId
     * @param procDefKey
     * @param businessKey
     * @return
     */
    @Override
    public String startByKey(String authenticateUserId, String procDefKey, String businessKey) {
        return this.startByKey(authenticateUserId, procDefKey, businessKey, null);
    }

    /**
     * 启动
     *
     * @param authenticateUserId
     * @param procDefKey
     * @param businessKey
     * @param variables
     * @return
     */
    @Override
    public String startByKey(String authenticateUserId, String procDefKey, String businessKey, Map<String, Object> variables) {
        return this.startProcessInstanceSupport.start(authenticateUserId, procDefKey, null, businessKey, variables);
    }

    /**
     * 启动流程
     *
     * @param processDefinitionId
     * @return
     */
    @Override
    public String startById(String processDefinitionId) {
        return this.startById(null, processDefinitionId);
    }

    /**
     * 启动流程
     *
     * @param processDefinitionId
     * @return
     */
    @Override
    public String startById(String authenticateUserId, String processDefinitionId) {
        return this.startById(authenticateUserId, processDefinitionId, null);
    }

    /**
     * 启动流程
     *
     * @param processDefinitionId
     * @return
     */
    @Override
    public String startById(String authenticateUserId, String processDefinitionId, String businessKey) {
        return startById(authenticateUserId, processDefinitionId, businessKey, null);
    }

    /**
     * 启动流程
     *
     * @param authenticateUserId
     * @param processDefinitionId
     * @param businessKey
     * @param variables
     * @return
     */
    @Override
    public String startById(String authenticateUserId, String processDefinitionId, String businessKey, Map<String, Object> variables) {
        return this.startProcessInstanceSupport.start(authenticateUserId, null, processDefinitionId, businessKey, variables);
    }

    /**
     * 完成任务
     *
     * @param taskId
     */
    @Override
    public void complete(String taskId) {
        this.complete(taskId, null, null, null, null);
    }

    /**
     * 完成任务
     *
     * @param taskId
     * @param variables
     */
    @Override
    public void complete(String taskId, Map<String, Object> variables) {
        this.completeTaskSupport.complete(taskId, null, variables, null, null);
    }

    /**
     * 完成任务
     *
     * @param taskId
     * @param variables
     */
    @Override
    public void complete(String taskId, Map<String, Object> variables, String procInstId, String message, String assignee) {
        this.completeTaskSupport.complete(taskId, procInstId, variables, message, assignee);
    }

    /**
     * 指派
     *
     * @param taskId
     * @param userId
     */
    @Override
    public void claim(String taskId, String userId) {
        this.claimTaskSupport.claim(taskId, userId);
    }

    /**
     * 取消指派
     *
     * @param taskId
     */
    @Override
    public void unClaim(String taskId) {
        this.claimTaskSupport.unClaim(taskId);
    }

    /**
     * 查询流程数据
     *
     * @param taskId
     * @return
     */
    @Override
    public WorkflowTaskData queryTaskById(String taskId) {
        return this.queryTaskSupport.queryTaskById(taskId);
    }

    /**
     * 查询流程数据
     *
     * @param procInstId
     * @return
     */
    @Override
    public WorkflowTaskData queryTaskByProcInstId(String procInstId) {
        return queryTaskSupport.queryTaskByProcInstId(procInstId);
    }

    /**
     * 查询组任务
     *
     * @param groupId
     * @return
     */
    @Override
    public List<WorkflowTaskData> queryGroupTask(String groupId) {
        return queryTaskSupport.queryGroupTask(groupId);
    }

    /**
     * 查询流程数据
     *
     * @param businessKey
     * @return
     */
    @Override
    public WorkflowTaskData queryTaskByBusinessKey(String businessKey) {
        return queryTaskSupport.queryTaskByBusinessKey(businessKey);
    }

    /**
     * 审批意见列表
     *
     * @param procInstId 流程实例id
     * @return
     */
    @Override
    public List<CommentData> queryMessageByProcInstId(String procInstId) {
        return queryHistoricTaskSupport.queryMessageByProcInstId(procInstId);
    }

    /**
     * 查询历史数据
     *
     * @param procInstId 流程实例id
     * @param taskId
     * @return
     */
    @Override
    public List<HistoryData> queryHistoryTaskDefKeyByProcInstId(String procInstId, String taskId) {
        return queryHistoricTaskSupport.queryHistoryTaskDefKeyByProcInstId(procInstId);
    }

    /**
     * 跳转至指定活动节点
     *
     * @param taskId
     * @param toNodeId
     * @throws Exception
     */
    @Override
    public void jump(String taskId, String toNodeId, String procInsId, String message, String assignee) {
        if (StringUtils.isBlank(taskId)) {
            throw new ActivitiException(ResultCode.ILLEGAL_PARAM.getCode(), "任务节点编号为空");
        }
        if (StringUtils.isBlank(toNodeId)) {
            throw new ActivitiException(ResultCode.ILLEGAL_PARAM.getCode(), "下一个节点名称为空");
        }
        completeTaskSupport.executeCommand(taskId, toNodeId, procInsId, message, assignee);
    }

    /**
     * 跳转至指定活动节点
     *
     * @param taskId
     * @param toNodeId
     * @throws Exception
     */
    @Override
    public void jump(String taskId, String toNodeId) {
        if (StringUtils.isBlank(taskId)) {
            throw new ActivitiException(ResultCode.ILLEGAL_PARAM.getCode(), "任务节点编号为空");
        }
        if (StringUtils.isBlank(toNodeId)) {
            throw new ActivitiException(ResultCode.ILLEGAL_PARAM.getCode(), "下一个节点名称为空");
        }
        completeTaskSupport.executeCommand(taskId, toNodeId, null, null, null);
    }

    /**
     * 判断是否挂起状态
     *
     * @param procInstId 流程实例id
     * @return
     */
    @Override
    public boolean isSuspended(String procInstId) {
        return startProcessInstanceSupport.isSuspended(procInstId);
    }

    /**
     * 挂起任务
     *
     * @param procInstId
     */
    @Override
    public void suspendByProcessId(String procInstId) {
        startProcessInstanceSupport.suspendByProcessId(procInstId);
    }

    /**
     * 删除流程
     */
    @Override
    public void deleteProcessInstance(String processId, String reason, String assignee) {
        startProcessInstanceSupport.deleteProcessInstance(processId, reason, assignee);
    }

    /**
     * 查询用户待办任务
     *
     * @param assignee 用户
     * @return 任务列表
     */
    @Override
    public List<TaskData> queryToDoTasks(String assignee) {
        return queryToDoTaskSupport.queryToDoTasks(assignee);
    }

    /**
     * 已办
     *
     * @param assignee 用户
     * @return 任务列表
     */
    @Override
    public List<TaskData> queryDoneTasks(String assignee) {
        return queryToDoTaskSupport.queryDoneTasks(assignee);
    }

    /**
     * 我发起的
     *
     * @param assignee 用户
     * @return 任务列表
     */
    @Override
    public List<TaskData> queryUserLaunchTask(String assignee) {
        return queryToDoTaskSupport.queryUserLaunchTask(assignee);
    }
}
